package arrAndstr;

/*
    82. 删除排序链表中的重复元素 II

    链表题别不舍得加变量
 */
public class T82 {
    public static class ListNode {
        int val;
        ListNode next;

        ListNode() {
        }

        ListNode(int val) {
            this.val = val;
        }

        ListNode(int val, ListNode next) {
            this.val = val;
            this.next = next;
        }
    }

    public static void main(String[] args) {
        ListNode head = new ListNode(1);
        ListNode node = head;

        node.next = new ListNode(1);
        node = node.next;

        deleteDuplicates(head);
    }

    public static ListNode deleteDuplicates(ListNode head) {
        // 先处理头指针
        while (true) {
            // 进出都会判定一次
            if (head == null || head.next == null) return head;

            if (head.val == head.next.val) {
                int temp = head.val;
                while (true) {
                    if (head == null || temp != head.val) break;
                    else {
                        head = head.next;
                    }
                }
            } else break;
        }

        // 再处理别的
        ListNode node = head;
        ListNode preNode = new ListNode();
        // 如果有两个元素一样，就删掉
        while (true) {
            if (node == null || node.next == null)   break;

            if (node.val==node.next.val) {
                int temp = node.val;
                while (true) {
                    if (node == null || node.val != temp) {
                        preNode.next = node;
                        break;
                    } else {
                        node = node.next;
                    }
                }
            } else {
                preNode = node;
                node = node.next;
            }
        }

        return head;
    }
}
